<template>
    <div class="mine-info">
        <!-- action上传地址 -->
        <el-upload
                class="avatar-uploader"
                action="123"
                accept=""
                :http-request="upLoad"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload">
            <img v-if="headUrl" :src="headUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>

    </div>
</template>

<script>
    import {faceUpdate, checkFace} from "@/api/facelogin";

    export default {
        name: "mine-info",
        data() {
            return {
                headUrl: ""
            };
        },
        methods: {
            upLoad(file) {
                let param = new FormData(); //创建form对象
                param.append('file', file.file);//通过append向form对象添加数据
                faceUpdate(param).then((data) => {
                    let {success, code, message} = data
                    success ? this.$message.success("人脸识别更新成功") : this.$message.error(message || "更新失败");
                }).catch((error) => {
                    this.$message.error(error)
                    console.log(error)
                })
            },
            handleSuccess(file) {
                console.log(file);
            },
            handleAvatarSuccess(res, file) {
                this.headUrl = URL.createObjectURL(file.raw);
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg'||'image/png';
                const isLt3M = file.size / 1024 / 1024 < 3;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG/PNG 格式!');
                }
                if (!isLt3M) {
                    this.$message.error('上传头像图片大小不能超过 3MB!');
                }
                return isJPG && isLt3M;
            },
        }
    }
</script>

<style scoped>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }

    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>